package waitnotify;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

@Slf4j
public class WaitNotifyTest {
    private static boolean flag = false;

    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                while (!flag) {
                    try {
                        log.info("t1发现条件依然不满足，进入wait。。。");
                        lock.wait();
                        log.info("t1被叫醒。。。");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (flag) {
                    log.info("t1被叫醒后发现条件满足，继续执行结束。。。");
                } else {
                    log.info("t1被叫醒后发现还是不满足，执行失败。。。");
                }
            }
        }, "t1");
        t1.start();
        Thread.sleep(3000);
        while (true) {
            synchronized (lock) {
                log.info("开始叫醒。。。");
                lock.notifyAll();
                try {
                    lock.wait(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
